﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_Reports_Accounts_OutStandings_Recivables : System.Web.UI.Page
{
     Datautility du=new Datautility();
    AccountSettings ac = new AccountSettings();
    double dbltotaloriginal = 0;
    double dbltotalreceived = 0;
    double dbltotalpending = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            hid.Value = Session["finyear"].ToString();
            lblcompname.Text = Session["compname"].ToString();
            txtToDate.Text = "31/03/" + (Convert.ToInt32(Session["FinYear"].ToString()) + 1).ToString();

            grid_grdDisplay(txtToDate.Text);
        }

    }
    protected void grid_grdDisplay(string strToDate)
    {
        SqlParameter[] pr = {new  SqlParameter("@GROUPTREEID","002003001006"),
                               new SqlParameter("@TODATE",string.Format("{0:MM-dd-yyyy}",Convert.ToDateTime(strToDate)))
                           };
        DataTable dt = du.getdatausingprocedure("USP_ACC_GetOutstandingDetails", pr);
        lblheading.Text = "Outstanding View for selected Ledgers as on " + string.Format("{0:dd-MM-yyyy}", Convert.ToDateTime(strToDate));
        grdDisplay.DataSource = dt;
        grdDisplay.DataBind();

    }

    protected void show(object sender, EventArgs e)
    {
        grid_grdDisplay(txtToDate.Text);
    }
    protected void rowdatabound(object sender, GridViewRowEventArgs e)
    {

        //string strTransactionDate=string.Empty;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink strLink = (HyperLink)e.Row.FindControl("hprvoucherno");
            string strTranstyepId = string.Empty;
            string strTransDate = string.Empty;
            string strTransId = string.Empty;

            if (e.Row.Cells[5].Text != string.Empty)
            {
                dbltotaloriginal += Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text));

            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) == 0)
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            else
            {
                e.Row.Cells[5].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text))).ToString();
            }

            if (e.Row.Cells[7].Text != string.Empty)
            {
                dbltotalpending += Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text));

            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text)) == 0)
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            else
            {
                e.Row.Cells[7].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text))).ToString();
            }
            if (e.Row.Cells[8].Text != string.Empty)
            {
                dbltotalreceived += Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text));

            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text)) == 0)
            {
                e.Row.Cells[8].Text = string.Empty;
            }
            else
            {
                e.Row.Cells[8].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text))).ToString();
            }

            strTranstyepId = grdDisplay.DataKeys[e.Row.RowIndex].Values[0].ToString();
            strTransDate = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(grdDisplay.DataKeys[e.Row.RowIndex].Values[1].ToString()));
            strTransId = grdDisplay.DataKeys[e.Row.RowIndex].Values[2].ToString();


            SqlParameter[] pr ={new  SqlParameter("@TYPEID",strTranstyepId)
                           };
            DataTable dt = du.getdatausingprocedure("USP_ACC_GetTransactionsSettings", pr);


            if (dt.Rows[0]["MODULETYPE"].ToString() == "1")//ACCOUNTS
            {
                string strFromDate = "04/01/" + Session["FinYear"].ToString();
                string strToDate = "03/31/" + (Convert.ToInt32(Session["FinYear"].ToString()) + 1).ToString();
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "2")
                {


                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString()      == "3")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "4")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }

                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "6")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "7")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "8")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "9")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "10")
                {
                    string strUrl = "../../../Accounts/VoucherPrint/Voucher_Print.aspx?key=" + strTransId + "&from=" + strFromDate + "&to=" + strToDate + "&transactiontype=" + strTranstyepId;

                    strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");
                }
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "11")
                {

                }


            }


            else if (dt.Rows[0]["MODULETYPE"].ToString() == "3")//Service Invoice
            {
                
                DataTable dt1 = new DataTable();
                string str = string.Empty;
               
                
                if (dt.Rows[0]["PARENTTYPEID"].ToString() == "2")
                {
                    str = "select CREDITNOTEID from IVC_CREDITNOTE_MAIN WHERE TRANID='" + strTransId + "'";
                    dt1 = du.getdata(str);
                    
                    if (dt1.Rows.Count > 0)
                    {
                        string strurl = "../../../ServiceInvoice/CreditNoteReport.aspx?creditnoteid=" + dt1.Rows[0]["CREDITNOTEID"].ToString() + "&tranid=" + strTransId;
                        strLink.Attributes.Add("onclick", "return openwindow('" + strurl + "');");
                    }
                }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "3")
                    {
                        str = "select DEBITNOTEID from IVC_DEBITNOTE_MAIN WHERE TRANID='" + strTransId + "'";
                        dt1 = du.getdata(str);
                        if (dt1.Rows.Count > 0)
                        {
                            string strurl = "../../../ServiceInvoice/DebitNote/DebitNoteReport.aspx?debitnoteid=" + dt1.Rows[0]["DEBITNOTEID"].ToString() + "&tranid=" + strTransId;
                            strLink.Attributes.Add("onclick", "return openwindow('" + strurl + "');");
                        }

                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "4")
                    {

                    }

                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "6")
                    {
                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "7")
                    {
                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "8")
                    {
                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "9")
                    {
                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "10")
                    {
                       
                        str = "select INVOICEID from IVC_INVOICE_MAIN WHERE TRANID='" + strTransId + "'";
                        dt1 = du.getdata(str);
                        if (dt1.Rows.Count > 0)
                        {

                            string strUrl = "../../../ServiceInvoice/SalesInvoiceReport.aspx?INVOICEID=" + dt1.Rows[0]["INVOICEID"].ToString() + "&TRANID=" + strTransId + "&ViewType=Original";

                            strLink.Attributes.Add("onclick", "return openwindow('" + strUrl + "');");

                        }

                    }
                    if (dt.Rows[0]["PARENTTYPEID"].ToString() == "11")
                    {
                    }

                }
                //else if(dt1.Rows[0]["MODULEID"] == "2")
                //{
                //    if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "2")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "3")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "4")
                //    {
                //    }

                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "6")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "7")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "8")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "9")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "10")
                //    {
                //    }
                //     if(dt1.Rows[0]["PARENTTYPEID"].ToString() == "11")
                //    {
                //    }

                //}


                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[4].Text = "Total";
                    e.Row.Cells[5].Text = ac.GetCurrency(dbltotaloriginal).ToString();
                    e.Row.Cells[7].Text = ac.GetCurrency(dbltotalpending).ToString();
                    e.Row.Cells[8].Text = ac.GetCurrency(dbltotalreceived).ToString();
                    e.Row.HorizontalAlign = HorizontalAlign.Right;
                    e.Row.Font.Bold = true;
                }




            }
        }
    

    protected void lnkback_Click(object sender, EventArgs e)
    {
        Response.Redirect("outstandings.aspx");
    }
}

